git 命令

删除远程分支

1
2
3
4
# As of Git v1.7.0, you can delete a remote branch using
git push origin --delete <branch_name>
# which is easier to remember than
git push origin :<branch_name>

git用得时间多了,会变慢。 那么可以用下面的命令优化一下

1
2
git gc - Cleanup unnecessary files and optimize the local repository
git-repack - Pack unpacked objects in a repository

那些git命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
git push origin test:test # 提交本地test分支作为远程的test分支
git push --tags # 上传所有标签
git remote set-url --push [name] [newUrl] # 修改远程仓库
git pull [remoteName] [localBranchName]
git push [remoteName] [localBranchName]
git merge [name] # 将名称为[name]的分支与当前分支合并
git rebase [name] #会将[name]分支的代码合并过来,并按照提交的顺序排序(衍合指定分支到分支到当前分支)
git merge-base branchA branchB #查看2分支的公共节点
git diff $(git-merge-base A B) B
# 按时间逆序列出提交对象,常用于查找涉及到某些文件的提交的hash
git rev-list
git shortlog
# 查看在branch上的,但不在master上的记录
git log branch --not master
git fsck --lost-found //检查丢失的提交
git ls-files --stage //可以显示出索引的内容
git checkout . //撤销所有修改
git clean -xfd //连 gitignore 的untrack 文件/目录也一起删掉
git branch --merged | xargs git branch -d //删除已经合并的分支
# list all ignored files in this project
git ls-files --other --ignored --exclude-stanard
# reset and preserve uncommitted local changes
git reset --keep $COMMIT
# create a new tracking branch based on a remote branch
git checkout --track $UPSTREAM/$REMOTE_BRANCH

View a file in a different Git branch without changing branches

1
2
3
4
git checkout $REVISION -- $FILE
git checkout $REVISION -- "*" switch all files without changing brnaches
git show $REVISION:$FILE
git checkout --orphan $BRANCH

Make an existing Git branch track a remote branch

1
2
3
4
5
6
7
8
9
10
git branch -u $UPSTREAM/$REMOTE_BRANCH $LOCAL_BRANCH
git branch --set-upstream-to=$UPSTREAM/$REMOTE_BRANCH
git push -u $UPSTREAM $REMOTE_BRANCH
git remote add $UPSTREAM <remote-url>
git fetch $UPSTREAM
git branch -f --track $LOCAL_BRANCH $UPSTREAM/$REMOTE_BRANCH
# OR:
git branch --set-upstream $LOCAL_BRANCH $UPSTREAM/$REMOTE_BRANCH

diff changes only among certain file(s)

1
2
3
4
git diff $BRANCH1 $BRANCH2 -- $FILE1 $FILE2
(branch1 is optional and your current branch (the branch you are on) will be considered by default if branch1 is not provided)
git diff $REVISION $FILE

other command

1
2
3
4
5
6
7
8
9
10
bisect #Find by binary search the change that introduced a bug
grep #Print lines matching a pattern
git log --since="two weeks ago" --until="two days ago"
git log --since=four.days --until=two.days
git instaweb
git ls-tree #view git tree view
git rm -rf . # Delete everything in the orphan branch
git stash save --keep-index # stash only unstaged files
git merge --squash $BRANCH
git rev-parse --abbrev-ref HEAD #判断当前分支

列出当天某人的所有提交记录

1
git log --author=AUTHOR --oneline --since="6am" --graph --all --decorate

Find the nearest parent branch of the current git branch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env zsh
branch=`git rev-parse --abbrev-ref HEAD`
git show-branch | ack '\*' | ack -v "$branch" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'`
# How it works:
# 1| Display a textual history of all commits.
# 2| Ancestors of the current commit are indicated
# by a star. Filter out everything else.
# 3| Ignore all the commits in the current branch.
# 4| The first result will be the nearest ancestor branch.
# Ignore the other results.
# 5| Branch names are displayed [in brackets]. Ignore
# everything outside the brackets, and the brackets.
# 6| Sometimes the branch name will include a ~2 or ^1 to
# indicate how many commits are between the referenced
# commit and the branch tip. We don't care. Ignore them.

or

1
2
branch=`git rev-parse --abbrev-ref HEAD`
git show-branch -a 2>/dev/null | grep '\*' | grep -v "$branch" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'

“^”代表父提交,当一个提交有多个父提交时,可以通过在”^”后面跟上一个数字,表示第几个父提交,”^”相当于”^1”

checkout只会移动HEAD指针,reset会改变HEAD的引用值